/******************************************************************************
 * $Id: toolbar.c,v 1.16 2004/09/22 01:14:23 jimb Exp $
 * @file
 * @author  Jim Brooks http://www.jimbrooks.org
 * @brief   Toolbar functions.
 ******************************************************************************/

#include "common.h"
#include "toolbar.h"
#include "selector.h"

#include "handlers.h"
#include "handlers_module_opened.h"
#include "text.h"

#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

/******************************************************************************
 * Create the toolbar.
 ******************************************************************************/
void
CreateToolbar( GtkWidget* pWidgetHbox )
{
   g_return_if_fail(pWidgetHbox);
  /*
   * Create the toolbar.
   */
   pWidgetToolbar = gtk_toolbar_new( );//GTK_ORIENTATION_HORIZONTAL,

   GtkWidget *image = NULL;

  /*
   * Create the toolbar button "Goto Line".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"goto_line.png");
   pWidgetToolbarButtonGotoLine = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _(" Goto Line "),
                             _("goto line (Ctrl+J)"),
                             NULL,
                             image,
                             G_CALLBACK(GotoLine),
                             NULL );
 //  gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

  /*
   * Create the toolbar button "Goto Tag".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"goto_tag.png");
   pWidgetToolbarButtonGotoTag = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _(" Goto Tag  F5 "),
                             _("goto tag under cursor (F5)"),
                             NULL,
                             image,	//NULL,
                             G_CALLBACK(HandlerToolbarButtonGotoTag),
                             NULL );
//   gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

  /*
   * Create the toolbar button "Earlier Tag".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"previous.png");
//   gtk_toolbar_prepend_space( GTK_TOOLBAR(pWidgetToolbar) );
   pWidgetToolbarButtonEarlierTag = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _(" Previous Tag  F6"),
                             _("jump to previous tag in history (F6)"),
                             NULL,
                             image,	//NULL,
                             G_CALLBACK(HandlerToolbarButtonEarlierTag),
                             NULL );

 //  gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

  /*
   * Create the toolbar button "Record Cursor".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"bookmark_add.png");
   gtk_toolbar_prepend_space( GTK_TOOLBAR(pWidgetToolbar) );
   pWidgetToolbarButtonRecordCursor = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _(" Record Cursor F9 "),
                             _("record cursor position into history (F9)"),
                             NULL,
                             image,
                             G_CALLBACK(HandlerToolbarButtonRecordCursor),
                             NULL );

 //  gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

  /*
   * Create the toolbar button "Later Tag".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"next.png");
   pWidgetToolbarButtonLaterTag = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _("Next Tag  F7"),
                             _("jump to next tag in history (F7)"),
                             NULL,
                             image,	//NULL,
                             G_CALLBACK(HandlerToolbarButtonLaterTag),
                             NULL );
 //  gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

  /*
   * Create the toolbar button "Find Matches".
   */
   image = gtk_image_new_from_file( IMAGE_DIR"find_all.png");
   pWidgetToolbarButtonFindMatches = \
   gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                             _(" Find Matches F8 "),
                             _("find all strings matching string under cursor (F8)"),
                             NULL,
                             image,	//NULL,
                             G_CALLBACK(HandlerToolbarButtonFindMatches),
                             NULL );

 //  gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );
#if 0
    image = gtk_image_new_from_file( IMAGE_DIR"refresh_proj.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                                 _("Refresh"),
                                 _("Refresh project"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_refresh_proj),
                                 NULL);
//   gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) ); 

   gtk_toolbar_append_space( GTK_TOOLBAR(pWidgetToolbar) );

    image = gtk_image_new_from_file( IMAGE_DIR"full_screen.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                                 _("Full Screen"),
                                 _("Full screen"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_full_screen),
                                 NULL);
                                 
    image = gtk_image_new_from_file( IMAGE_DIR"hide_tags.gif");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                                 _("Hide/Show"),
                                 _("Hide/Show tags"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_hide_tags),
                                 NULL);

    image = gtk_image_new_from_file( IMAGE_DIR"hide_2.gif");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar),
                                 _("Hide/Show"),
                                 _("Hide/Show left/right and bottom"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_hide_left_bottom),
                                 NULL);
#endif
   gtk_toolbar_set_icon_size(GTK_TOOLBAR(pWidgetToolbar),
		                GTK_ICON_SIZE_SMALL_TOOLBAR);
   gtk_toolbar_set_style(GTK_TOOLBAR(pWidgetToolbar),
						GTK_TOOLBAR_ICONS);
//						GTK_TOOLBAR_BOTH_HORIZ);
   gtk_toolbar_set_show_arrow(GTK_TOOLBAR(pWidgetToolbar),
							  TRUE);

  /*
   * Put the toolbar in the hbox.
   *
   * gtk_box_start() will place toolbar near to menubar (towards left).
   * gtk_box_end() wil place toolbar always at extreme right, and toolbar will move
   * right when main widget is resized wider.
   */
   gtk_box_pack_start( GTK_BOX( pWidgetHbox ),
                       pWidgetToolbar,
                       FALSE, FALSE, 0 );

   return;
}

gboolean
cb_ToolbarMenu( GtkToolbar *pToolbar,
                gint        x,
                gint        y,
                gint        button,
                gpointer    data)
{
    g_print("%s :%d\t%d\t%d\t%s\n",__func__,x,y,button,(char*)data);
    return TRUE;
}



static void
cb_AppendFunc( funcDef_t * pFuncDef,
               GList     **ppList)
{
    if ( NULL == pFuncDef ||
         NULL == ppList )
       return;

    GtkWidget *pListItem;
    pListItem = gtk_list_item_new_with_label(pFuncDef->pTagFuncDef->pName);
    
    if ( NULL == pListItem )
        return;

    /*
     * append the functions tag into menu_item
     */
    g_object_set_data( G_OBJECT(pListItem),
                       "FuncDef",               /* key   */
                       pFuncDef->pTagFuncDef);  /* value */
    
    *ppList = g_list_append( *ppList, G_OBJECT( pListItem ) );
    
    return;
}


/*
 * create the GList for the GtkCombo's list
 */
GList*
CreateComboFuncList( module_t * pModule )
{
    if ( NULL == pModule )
        return NULL;

    if ( 0 == g_slist_length( pModule->pGslistFuncDef ) )
        return NULL;    
    
    GList * pGList = NULL;
    
    g_slist_foreach( pModule->pGslistFuncDef,
                     (GFunc)cb_AppendFunc,
                     &pGList );

    
    return pGList;
}



static void
cb_print( //gpointer pKey,
          funcDef_t* pfunc,
          gpointer   pUnUsed)
{
    if ( NULL == pfunc )
        return;

    g_print("%s\t\t%s\n",pfunc->pTagFuncDef->pType,
                         pfunc->pTagFuncDef->pName);
}

/* for test */
void
cb_ToolbarButton( GtkWidget * button,
                  GtkWidget * pFuncListMenu)
{
    module_t * pModule = ActiveModule();

    if ( !pModule )
        return;
    printf("size of long=%d sizeof int=%d sizeof short int=%d\n",sizeof(long),
                sizeof(int), sizeof(short int));

    g_slist_foreach( pModule->pGslistFuncDef,
                    (GFunc)cb_print,
                    NULL);
}


void
cb_ToolbarFuncList( GtkList       *pWidgetList,
                    GtkListItem   *pItem,
                    GtkCombo      *pWidgetCombo )
{
    if ( NULL == pWidgetList || NULL == pItem )
        return;
    gtk_list_unselect_child( GTK_LIST( pWidgetList ),
                             GTK_WIDGET(pItem ) );
    
    /*
     * clear up the combo-list's input box
     */
    gtk_entry_set_text( GTK_ENTRY(GTK_COMBO( pWidgetComboFuncList )->entry),
                        "" );

    /*
     * Get the function's tag
     */
    tag_t * pTagFuncDef_ = g_object_get_data( G_OBJECT( pItem ),
                                              "FuncDef" );
    if ( NULL == pTagFuncDef_ )
        return;
    
    DebugMsg( "**Welcome to %s\n", __func__ );

    ScrollTextWidgetToLine( pTagFuncDef_->lineInt,
                            SCROLL_TEXT_VERT_FRAC );
    
    HighlightTag( pTagFuncDef_,
                  -1 );
    //HighlightTag( pTagFuncDef,
    //              123 );
    return;
}


void
RefreshToolbarFuncList( module_t * pModule )
{
    static GList *  pItems = NULL;
    static gboolean thisBusy = FALSE;
    
    if ( thisBusy )
        return;
        
    thisBusy = TRUE;

    
    if ( NULL == pModule || NULL == pWidgetComboFuncList )
        goto quit;
        
#if 0    
    /*
     * block the handler of pWidgetOptionMenuForToolbar
     */
    g_signal_handler_block( pWidgetOptionMenuForToolbar,
                            sigIdWidgetOptionMenuForToolbar );
#endif

    GtkList * pList ;

    pList = GTK_LIST( GTK_COMBO( pWidgetComboFuncList )->list );
    if ( NULL == pList )
        goto quit ;

    // remove exists func list
    if ( NULL != pItems )
        gtk_list_remove_items( pList ,
                               pItems );

    // get new func list
    pItems = CreateComboFuncList( pModule );
    if ( NULL != pItems )
        gtk_list_append_items( pList,
                               pItems );

    gtk_list_set_selection_mode ( pList,
                                  GTK_SELECTION_SINGLE );

    /*
     * clear up the combo-list's input box
     */
    gtk_entry_set_text( GTK_ENTRY(GTK_COMBO( pWidgetComboFuncList )->entry),
                        "" );

    gtk_widget_show_all( GTK_WIDGET(pList ));

#if 0
    g_signal_handler_unblock( pWidgetOptionMenuForToolbar,
                              sigIdWidgetOptionMenuForToolbar );
#endif

quit:
    thisBusy = FALSE;
}


void
cb_a( GtkEntry *entry,
      gpointer  data )
{
    static int i=0;
    DebugMsg("=======%d", i++);
}


/******************************************************************************
 * Create the toolbar.
 ******************************************************************************/
void
CreateToolbar2( GtkWidget* pWidgetHBox )
{
   g_return_if_fail(pWidgetHBox);
   
   GtkWidget *image = NULL;
   
   // create pWidgetToolbar2
   pWidgetToolbar2 = gtk_toolbar_new();
//
    image = gtk_image_new_from_file( IMAGE_DIR"new_file.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("New"),
                                 _("New file"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_new_file),
                                 NULL);
/*
    image = gtk_image_new_from_file( IMAGE_DIR"open.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("Open"),
                                 _("Open file"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_open_file),
                                 NULL);
*/
    image = gtk_image_new_from_file( IMAGE_DIR"save_file.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("Save"),
                                 _("Save file"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_save_file),
                                 NULL);

    image = gtk_image_new_from_file( IMAGE_DIR"save_file_as.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("Save as"),
                                 _("Save file as"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_save_file_as),
                                 NULL);

    image = gtk_image_new_from_file( IMAGE_DIR"save_file_all.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("Save all"),
                                 _("Save all files"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_save_all_file),
                                 NULL);

    image = gtk_image_new_from_file( IMAGE_DIR"edit_enable.png");
    gtk_toolbar_append_item ( GTK_TOOLBAR(pWidgetToolbar2),
                                 _("Edit"),
                                 _("Edit enable/disable"),
                                 NULL,
                                 image,
                                 G_CALLBACK(cb_editable),
                                 NULL);


   /*
    * GtkCombo has a GtkList, when we refresh the func list, 
    * we register a selection-changed signal to it
    */
   pWidgetComboFuncList = gtk_combo_new ();

//   gtk_combo_disable_activate( pWidgetComboFuncList );

   g_signal_connect( GTK_COMBO(pWidgetComboFuncList)->entry,
                     "changed",//"activate",
                     G_CALLBACK(cb_a),
                     pWidgetComboFuncList );    // the Combo object
//    gtk_entry_set_editable( GTK_COMBO(pWidgetComboFuncList)->entry,
//                            FALSE );
                     
   /*
    * GtkCombo has a GtkList, when we refresh the func list, 
    * we register a selection-changed signal to it
    */
   g_signal_connect( GTK_COMBO(pWidgetComboFuncList)->list,
                     "select-child",
                     G_CALLBACK(cb_ToolbarFuncList),
                     pWidgetComboFuncList );    // the Combo object
/*
    g_signal_connect( GTK_COMBO(pWidgetComboFuncList)->list,
                     "selection-changed",
                     G_CALLBACK(cb_ToolbarFuncList),
                     pWidgetComboFuncList );    // the Combo object
 */                   
    gtk_toolbar_append_widget( GTK_TOOLBAR(pWidgetToolbar2),
                               pWidgetComboFuncList,
                               "combo list",
                               NULL );


/*

    GtkWidget * toolbarButton ;
    toolbarButton = gtk_button_new_with_label(_("test button"));
    g_signal_connect(G_OBJECT(toolbarButton),
                     "clicked",
                     G_CALLBACK(cb_ToolbarButton),
                     NULL);
    gtk_toolbar_append_widget(GTK_TOOLBAR(pWidgetToolbar2),
                              toolbarButton,
                              _("print current file's function list"),
                              NULL);    // when clicked, update it

 */

    /*
     * toolbar context menu
     */
    g_signal_connect(G_OBJECT(pWidgetToolbar2),
                     "popup-context-menu",
                     G_CALLBACK(cb_ToolbarMenu),
                     "toolbar menu");

   gtk_toolbar_prepend_space( GTK_TOOLBAR(pWidgetToolbar2) );


   gtk_toolbar_set_icon_size(GTK_TOOLBAR(pWidgetToolbar2),
		                GTK_ICON_SIZE_SMALL_TOOLBAR);
   gtk_toolbar_set_style(GTK_TOOLBAR(pWidgetToolbar2),
						GTK_TOOLBAR_ICONS);
//						GTK_TOOLBAR_BOTH_HORIZ);
   gtk_toolbar_set_show_arrow(GTK_TOOLBAR(pWidgetToolbar2),
							  TRUE);
							  
   // second toolbar
   gtk_box_pack_start( GTK_BOX( pWidgetHBox),
                       pWidgetToolbar2,
                       FALSE, FALSE, 0 );

}//createtoolbar2
